$(function(){

	//取当前控制器名称函数
	function Contr(){
			//取出当前URL
			var ContrStr = window.location.href;
			
			//切字符串
			ContrStr = ContrStr.split('/');
			//alert(ContrStr);
			//取得控制器名
			var pre = ContrStr.indexOf("admin");
			var next = ContrStr.indexOf("index");
			
			
			var controller = ContrStr[pre+1];
			//alert(controller);
			return 	controller;
		}

		//首字母转换成大写
		function newUpper(str){
			var NewStr = str.split('');
			var First_Char = NewStr['0'];
			var A_Char = First_Char.toUpperCase()+''+NewStr['1']+''+NewStr['2']+''+NewStr['3'];
			return A_Char;
		}

        //取出当前的URL
    function GetUrl(){
        var ContrStr = window.location.href;
        var NewContrStr = ContrStr.split('/');
        //alert(NewContrStr);
        var str = '';
       for(var i = 0; i < NewContrStr.length; i++){

         
         if(NewContrStr[i] == 'index' || NewContrStr[i] == 'admin'){
            str += (NewContrStr[i] + '/');
                return str;
         }
         str += (NewContrStr[i] + '/');
       }
    }
    
    var AjaxUrl = GetUrl();

	//AJAX发送函数
    function AjaxSend(Action,str,controller,cont,AjaxUrl){
        //alert("发送AJAXing!!!");
        
        //alert(a);
        $.ajax({  
            datatype:"json",
                    type : "POST",  //提交方式  
                     url : AjaxUrl + '' + controller +"/"+Action+cont,//路径 
                    //url : "http://localhost/AdminFrame/public/admin/user/"+Action+"cate",//路径 
                    data : {  
                        "userinfo_str":str

                    },//数据，这里使用的是Json格式进行传输  
                    success : function(result) {//返回数据根据结果进行相应的处理                        
                        //alert("ok");
                        //转成json 字符串
                        var ResultJsonObject = $.parseJSON(result);                                            	                        
                        //var a = $('#content .weather_block > div > h3').html();
                        //alert(a);
						alert('修改成功');
						window.location.reload();                        	
                                          
                    },
                    error : function(result){

                        alert("修改失败");
                        window.location.reload();
                        
                    } 
                }); 
    }

   
//iCheck插件的全选与全不选
$('#theadcheck').on('ifChecked ifUnchecked', function(event) {
	if (event.type == 'ifChecked') {
		$('input[name = "checkbox1"]').iCheck('check');

	} else {
            // checkboxes.iCheck('uncheck');
            $('input[name = "checkbox1"]').iCheck('uncheck');
            
        }
    });

//得到模态对话框的修改内容并发送
//监听SAVEMODIFY按钮动作

$('#myModal .modal-footer button[name = "savemodify"]').click(function(){
	//alert("按下了修改键");
	//读取DIALOG里的input内容	
	//取出当前控制器名称
	var sure = confirm('是否确认修改？')
	if(!sure){
		alert('修改已取消');
		window.location.reload();
		return ;	
	}
	var controller = Contr();

	var cont = newUpper(controller);		
	
	var InputIndex = ['id','username','create_time','phone','memberlevel','identitynum','email'];
	
	//var taget = "$('.modal-body input[name ='+ InputIndex[i] +']')";
	Action = "modify";
	var str = "";
	// var InputIndex = ['id','username','password','create_time','update_time','ip','email'];
	//alert(1);
	//alert(taget);
	//alert($('.modal-body input[name ="id"]').prop('value'));

	for(var i = 0; i < InputIndex.length; i++){
		if(($('#myModal .modal-body input[name ='+ InputIndex[i] +']').prop('value'))){
			//alert("存在");
			str += $('#myModal .modal-body input[name ='+ InputIndex[i] +']').prop('value') + ",";
		}else{
			str += $('#myModal .modal-body input[name ='+ InputIndex[i] +']').attr('placeholder') + ",";
		}
		//str += (taget.attr('value') ? taget.attr('value') : taget.attr('placeholder')) + ",";
	}
	//alert(str);
	//末尾的逗号去掉
	if(str.substr(str.length-1) == ','){
		str = str.substr(0,str.length-1);
	}
	//调用AJAX 函数发送
	//alert(str);

	AjaxSend(Action,str,controller,cont,'/admin/');
});
//alert(1);


//iCheck插件取值,发送AJAX
$('button[name ="ActionButton"]').click(function(){

	var Action = $(this).siblings('select').val();

	if(Action == "修改"){
		Action = "modify";
		$(this).attr('data-target' , "#myModal");
	}else if(Action == "删除"){
    	Action = "delete";
    	$(this).attr('data-target' , "");
    }else{
    	alert("请选择操作内容！！！");
    }

	//发送标识符
    var accept = "0";
    //发送字符串
    var str="";

    if(Action == "modify"){

    	//alert(1);
    	$(".checked").parent().siblings('td').each(function(){

    		//遍历每个TD的内容
        	str +=	$(this).not(".tdbox").html() +",";
        	
    	})

    	//alert(str);   

        var ModifyArray = str.split(',');	
      	
      	//alert(ModifyArray);

      	//末尾的逗号去掉
        if(str.substr(str.length-1) == ','){
            str = str.substr(0,str.length-1);
        }        
        	var InputIndex = ['id','username','create_time','phone','memberlevel','identitynum','email'];
       
        //alert(InputIndex['2']);
        //alert(InputIndex[0]);
        var a = InputIndex.length;        
        for(var i = 0; i < InputIndex.length; i++){
        	
        	$('#myModal .modal-dialog .modal-body input[name = '+ InputIndex[i]+']').attr('placeholder',ModifyArray[i]);
        }
	}
	/******如果是删除动作*******/
     if(Action == "delete"){
     	var sure = confirm('是否确认删除？')
		if(!sure){
		alert('删除已取消');
		window.location.reload();
		return ;	
	}
     	$("input[name='checkbox2']:checkbox").each(function(){
     		if(true == $(this).is(':checked')){
        	//弹出undefine
        	if($(this).parent().parent().siblings("td[name = 'id']").html()){

        		str += $(this).parent().parent().siblings("td[name = 'id']").html() +",";
        	}
        }
    });
    	//末尾的逗号去掉
    	if(str.substr(str.length-1) == ','){
    		str = str.substr(0,str.length-1);
    	}
    	//alert(str);
    	var controller = Contr();
    	var cont = newUpper(controller);
    	accept = "1";
    	if(str.length != 0 && accept == "1"){
    	//alert("准备发送");
    	AjaxSends(Action,str,controller,cont);
    	
    }
    }
})
 

})